﻿var urls = "http://localhost/WebApi/api/";
var OPERATETOR = '';
var OPERATETORNAME = '';

$(function () {

    OPERATETOR = getUrlParam('usercode');
    OPERATETORNAME = getUrlParam('username');

    FWload();
})

// 加载数据
function FWload() {
    $('#table').bootstrapTable({
        method: "POST",
        url: urls + "JobInfo/InfoManger",
        //url: "../Ashx/Quartz_handler.ashx?action=GetInfo",
        striped: true,
        singleSelect: false,
        dataType: "json",
        sidePagination: "server",//服务端分页http://localhost:61995/calendar
        pagination: true, //分页
        pageSize: 10,
        pageNumber: 1,
        search: false, //显示搜索框
        contentType: "application/x-www-form-urlencoded",
        queryParams: null,
        columns: [
            {
                title: "",
                checkbox: "true",
                align: 'center',
                valign: 'middle',
                width: '5%'
            },
            {
                field: 'NGUID',
                align: 'center',
                valign: 'middle',
                visible: false
            },
            //{
            //    title: '用车日期',
            //    field: 'YCRQ',
            //    align: 'center',
            //    valign: 'middle',
            //    width: '10%',
            //    formatter: function (value, row) {
            //        value = value.split(' ')[0];
            //        return value;
            //    }
            //},
             {
                 title: '任务名称',
                 field: 'JOBNAME',
                 align: 'center',
                 valign: 'middle',
                 width: '5%'
             },
             {
                 title: '任务组名称',
                 field: 'JOBGROUPNAME',
                 align: 'center',
                 valign: 'middle',
                 width: '10%',
                 visible: false
             },
             {
                 title: '触发器名称',
                 field: 'TRIGGERNAME',
                 align: 'center',
                 valign: 'middle',
                 width: '10%',
                 visible: false
             },
             {
                 title: '触发器组名称',
                 field: 'TRIGGERGROUPNAME',
                 align: 'center',
                 valign: 'middle',
                 width: '10%',
                 visible: false
             },
             {
                 title: '执行程序名称',
                 field: 'JOBSNAME',
                 align: 'center',
                 valign: 'middle',
                 width: '10%'
             },
             {
                 title: '程序路径',
                 field: 'JOBPATH',
                 align: 'center',
                 valign: 'middle',
                 width: '10%',
                 visible: false
             },
             {
                 title: '执行时间',
                 field: 'JOBCRON',
                 align: 'center',
                 valign: 'middle',
                 width: '10%'
             },
             {
                 title: '任务简介',
                 field: 'JOBJJ',
                 align: 'center',
                 valign: 'middle',
                 width: '10%'
             },
             {
                 title: '运行id',
                 field: 'RUNGUID',
                 align: 'center',
                 valign: 'middle',
                 width: '10%',
                 visible: false
             },
             {
                 title: '上次运行时间',
                 field: 'LASTTIME',
                 align: 'center',
                 valign: 'middle',
                 width: '10%'
             },
             {
                 title: '下次运行时间',
                 field: 'NEXTTIME',
                 align: 'center',
                 valign: 'middle',
                 width: '10%'
             },
             {
                 title: '序号',
                 field: 'XH',
                 align: 'center',
                 valign: 'middle',
                 width: '10%'
             },
            {
                title: '任务状态',
                field: 'RUNSTATUS',
                align: 'center',
                valign: 'middle',
                width: '10%',
                formatter: function (value, row) {
                    if (row.RUNSTATUS == "1") {
                        return "正在运行中";
                    } else if (row.RUNSTATUS == "2") {
                        return "等待下次运行";
                    } else {
                        return "暂停中";
                    }
                }
            }
            //{
            //    title: '申请科室',
            //    field: 'SQKS',
            //    align: 'center',
            //    valign: 'middle',
            //    width: '10%',
            //    style: 'overflow: hidden;text-overflow: ellipsis;white-space: nowrap',
            //    formatter: function (value, row) {
            //        if (ChechNull(row.NGUID)) {
            //            return value;
            //        } else {
            //            var url = '<a href="#"  style="color:#23527c;white-space:nowrap"  onclick="View(\'' + row.NGUID + '\')">' + value + '</a>  ';
            //            return url;
            //        }

            //    }
            //},


        ],
        // 请求服务器数据时发送的参数，可以在这里添加额外的查询参数，返回false则终止请求
        queryParams: function queryParams(params) {
            var temp = {
                offset: params.offset,  //页码              
                pageNumber: this.pageNumber,
                pageSize: this.pageSize,// 每页要显示的数据条数 
                OPERATETOR: OPERATETOR,
                OPERATETORNAME: OPERATETORNAME,
                //SQKSBH: $('#SQKSBH').val(),
                //CLLX: $("#CLLX").val(),
                //CPH: $("#CPH").val(),
                //HyStart: $("#startTime").val(),
                //HyEnd: $("#endTime").val(),
                //MDDD: $("#MDDD").val(),
                runNumber: Math.random()
            };
            return temp;
        }
    });
}

//添加-1
function add(obj) {
    lyrId = layer.open({
        type: 2,
        title: '添加信息',
        shadeClose: true,
        shade: 0.5,
        skin: 'layui-layer-rim',
        closeBtn: 2,
        area: ['68%', '85%'],
        shadeClose: true,
        closeBtn: 2,
        content: 'QuartzJobAdd.html?id=' + obj + '&type=add&usercode=' + OPERATETOR + '&username=' + OPERATETORNAME,
        end: function (index, layero) {
            $('#table').bootstrapTable('destroy');
            $('#table').bootstrapTable('refresh', {
                pageNumber: 1
            });
            FWload();
        }
    });

}

function edit() {
    //获取所有被选中的公文
    var rows = $("#table").bootstrapTable('getSelections');
    if (rows.length == 0) {
        layer.msg('请先选择要修改的数据!', {
            icon: 7
        });
        return;
    } else if (rows.length > 1) {
        layer.msg('只能选择一条数据!', {
            icon: 7
        });
        return;
    }
    if (rows[0]['RUNSTATUS'] != "3") {
        layer.msg('请先暂停任务!', {
            icon: 7
        });
        return;
    }

    var ids = '';
    ids = rows[0]['NGUID'];

    lyrId = layer.open({
        type: 2,
        title: '添加信息',
        shadeClose: true,
        shade: 0.5,
        skin: 'layui-layer-rim',
        closeBtn: 2,
        area: ['68%', '85%'],
        shadeClose: true,
        closeBtn: 2,
        content: 'QuartzJobAdd.html?id=' + ids + '&type=edit&usercode=' + OPERATETOR + '&username=' + OPERATETORNAME,
        end: function (index, layero) {
            $('#table').bootstrapTable('destroy');
            $('#table').bootstrapTable('refresh', {
                pageNumber: 1
            });
            FWload();
        }
    });

}

function Delete() {
    //获取所有被选中的公文
    var rows = $("#table").bootstrapTable('getSelections');
    if (rows.length == 0) {
        layer.msg('请先选择要删除的任务!', {
            icon: 7
        });
        return;
    } else if (rows.length > 1) {
        layer.msg('只能选择一条数据!', {
            icon: 7
        });
        return;
    }
    if (rows[0]['RUNSTATUS'] != "3") {
        layer.msg('请先暂停任务!', {
            icon: 7
        });
        return;
    }

    var ids = '';
    ids = rows[0]['NGUID'];
    layer.confirm('请问您确定要删除吗？', {
        btn: ['确定', '取消'], //按钮
        icon: 3
    }, function () {
        //ids = ids.substring(0, ids.length - 1);
        $.ajax({
            type: 'Get',
            url: urls + "JobInfo/UpdateJobValid",
            data: {
                NGUID: ids,
                OPERATETOR: OPERATETOR,
                OPERATETORNAME: OPERATETORNAME
            },
            dataType: "json",
            async: false,
            success: function (data) {
                if (data.code == 200) {
                    parent.layer.msg("删除成功", {
                        icon: 1
                    });
                    $('#table').bootstrapTable('destroy');
                    $('#table').bootstrapTable('refresh', {
                        pageNumber: 1
                    });
                    FWload();
                } else {
                    parent.layer.msg(data.msg, {
                        icon: 2
                    });
                }
            }
        })
    })

}

function UpdateStatus(runStatus) {


    var rows = $("#table").bootstrapTable('getSelections');
    if (rows.length == 0) {
        layer.msg('请先选择要暂停的任务!', {
            icon: 7
        });
        return;
    }
    var ids = '';
    for (var i = 0; i < rows.length; i++) {
        if (rows[i]['RUNSTATUS'] == "1") {
            layer.msg('所选任务有正在运行，不可暂停!', {
                icon: 7
            });
            return;
        }
        if (runStatus == "3") {
            if (rows[i]['RUNSTATUS'] == "3") {
                layer.msg('所选任务有的已经暂停，不可再次暂停!', {
                    icon: 7
                });
                return;
            }
        }

        ids += rows[i]['NGUID'] + "^";
    }

    ids = ids.substring(0, ids.length - 1);
    $.ajax({
        type: "POST",
        url: urls + "JobInfo/UpdateRunStatus",
        contentType: 'application/json',
        data: JSON.stringify({
            NGUID: ids,
            runStatus: runStatus,
            OPERATETOR: OPERATETOR,
            OPERATETORNAME: OPERATETORNAME
        }),
        dataType: "json",
        async: false,
        success: function (data) {
            if (data.code == 200) {
                if (runStatus == "3") {
                    parent.layer.msg("暂停成功", {
                        icon: 1
                    });
                } else if (runStatus == "2") {
                    parent.layer.msg("启动成功", {
                        icon: 1
                    });
                }
                $('#table').bootstrapTable('destroy');
                $('#table').bootstrapTable('refresh', {
                    pageNumber: 1
                });
                FWload();
            } else {
                parent.layer.msg(data.msg, {
                    icon: 2
                });
            }
        }
    })
}

function UpdateISChange(runStatus) {


    var rows = $("#table").bootstrapTable('getSelections');
    if (rows.length == 0) {
        layer.msg('请先选择要启动的任务!', {
            icon: 7
        });
        return;
    } else if (rows.length > 1) {
        layer.msg('只能选择一条任务!', {
            icon: 7
        });
        return;
    }
    if (rows[0]['RUNSTATUS'] != "3") {
        layer.msg('请先暂停任务!', {
            icon: 7
        });
        return;
    }

    var ids = '';
    ids = rows[0]['NGUID'];

    $.ajax({
        type: "POST",
        url: urls + "JobInfo/UpdateISChange",
        contentType: 'application/json',
        data: JSON.stringify({
            NGUID: ids,
            runStatus: runStatus,
            OPERATETOR: OPERATETOR,
            OPERATETORNAME: OPERATETORNAME
        }),
        dataType: "json",
        async: false,
        success: function (data) {
            if (data.code == 200) {

                parent.layer.msg("启动成功", {
                    icon: 1
                });

                $('#table').bootstrapTable('destroy');
                $('#table').bootstrapTable('refresh', {
                    pageNumber: 1
                });
                FWload();
            } else {
                parent.layer.msg(data.msg, {
                    icon: 2
                });
            }
        }
    })
}

function getUrlParam(paras) {
    var url = window.location.href;
    var paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
    var paraObj = {}
    for (i = 0; j = paraString[i]; i++) {
        paraObj[j.substring(0, j.indexOf("=")).toLowerCase()] = j.substring(j.indexOf("=") + 1, j.length);
    }
    var returnValue = paraObj[paras.toLowerCase()];
    if (typeof (returnValue) == "undefined") {
        return "";
    } else {
        returnValue = decodeURI(returnValue);//解决中文乱码问题
        return returnValue;
    }
}